package cc.xulu.controller;

import com.jfinal.core.Controller;

import cc.xulu.model.*;

public class PostController extends Controller {
	public void index() {
		setAttr("postPage", Post.dao.paginate(getParaToInt(0, 1), 10, "select *", "from post order by id asc"));
		render("list.html");
	}
	
	public void add() {
	    setAttr("category", Category.dao.find("select * from category"));
	    setAttr("user", User.dao.find("select * from user"));
	    render("edit.html");
	}
	
	public void save() {
		Post post = getModel(Post.class);
		if (post.get("id") == null) {
			post.save();
		} else {
			post.update();
		}
		redirect("/post");
	}
	
	public void edit() {
		setAttr("category", Category.dao.find("select * from category"));
		setAttr("user", User.dao.find("select * from user"));
		setAttr("post", Post.dao.findById(getParaToInt()));
		render("edit.html");
	}
	
	public void update() {
		getModel(Post.class).update();
		redirect("/post");
	}
	
	public void delete() {
		Post.dao.deleteById(getParaToInt());
		redirect("/post");
	}
}
